package com.renren.webmaven2.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;

import com.renren.eliya.domain.BigClass;
import com.renren.eliya.domain.SmallClass;
import com.renren.webmaven2.domain.User;
import com.renren.webmaven2.service.BigClassService;
import com.renren.webmaven2.service.SmallClassService;
import com.renren.webmaven2.util.CacheUtils;
import com.renren.webmaven2.util.Constants;

/**
 */
@Controller
@SuppressWarnings("unchecked")
public class AdminClassController extends LocalBaseController {

	@Autowired
	private BigClassService bigClassService;
	@Autowired
	private SmallClassService smallClassService;

	@RequestMapping(value = "admin_base")
	public String adminBase(HttpServletRequest request, Model model) {

		User user = (User) request.getSession().getAttribute(Constants.USER_COOKIE);
		if (user == null) {
			model.addAttribute("gourl", "admin_base");
			return "login";
		}

		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		List<SmallClass> smallClasses = (List<SmallClass>) CacheUtils.get(Constants.NET_CACHE_ALL_SMALLCLASS);
		model.addAttribute("bigClasses", bigClasses);
		model.addAttribute("smallClasses", smallClasses);
		return "admin_base";
	}

	@RequestMapping(value = "login")
	public String login(HttpServletRequest request, Model model) {
		return "login";
	}

	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public RedirectView doAddPrice(HttpServletRequest request) throws InterruptedException {
		String password = getParameter(request, "password");
		String gourl = getParameter(request, "gourl");
		if (StringUtils.isNotBlank(password) && password.equals(Constants.PASSWORD)) {
			User user = new User();
			request.getSession().setAttribute(Constants.USER_COOKIE, user.myUser("1", "jizhong"));
			if (StringUtils.isNotBlank(gourl)) {
				return new RedirectView(gourl);
			} else {
				return new RedirectView("admin_base");
			}
		}
		return new RedirectView("login");
	}

	@RequestMapping(value = "/addBigClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addBigClass(HttpServletRequest request) throws InterruptedException {
		String name = request.getParameter("big_class_name");
		String weight = request.getParameter("big_class_weight");
		int result = bigClassService.add(name, weight);

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);

		return modelMap;
	}

	@RequestMapping(value = "/selectSmallClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> selectSmallClass(HttpServletRequest request) throws InterruptedException {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			String big_class_id = request.getParameter("big_class_id");
			List<SmallClass> smallClasses = smallClassService.selectSmallClass(Integer.valueOf(big_class_id));
			modelMap.put("smallClasses", smallClasses);
			modelMap.put("success", 1);
		} catch (Exception e) {
			modelMap.put("success", 0);
		}

		return modelMap;
	}

	@RequestMapping(value = "/updateBigClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> updateBigClass(HttpServletRequest request) throws InterruptedException {
		String id = request.getParameter("id");
		String bigClassName = request.getParameter("bigClassName");
		String bigClassWeight = request.getParameter("bigClassWeight");
		int result = bigClassService.update(bigClassName, Integer.valueOf(bigClassWeight), Integer.valueOf(id));

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);

		return modelMap;
	}

	@RequestMapping(value = "/updatesmallClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> updatesmallClass(HttpServletRequest request) throws InterruptedException {
		String id = request.getParameter("id");
		String smallClassName = request.getParameter("smallClassName");
		String smallClassWeight = request.getParameter("smallClassWeight");
		int result = smallClassService.update(smallClassName, Integer.valueOf(smallClassWeight), Integer.valueOf(id));

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);

		return modelMap;
	}

	@RequestMapping(value = "/delBigClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delBigClass(HttpServletRequest request) throws InterruptedException {
		String id = request.getParameter("id");
		int result = bigClassService.del(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/addsmallClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addsmallClass(HttpServletRequest request) throws InterruptedException {
		String name = request.getParameter("small_class_name");
		String weight = request.getParameter("small_class_weight");
		String big_class_id = request.getParameter("big_class_id");
		String isIndexView = request.getParameter("isIndexView");
		String small_class_content = request.getParameter("small_class_content");
		int result = smallClassService.add(name, weight, Integer.valueOf(big_class_id), Integer.valueOf(isIndexView), small_class_content);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/delsmallClass", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delsmallClass(HttpServletRequest request) throws InterruptedException {
		String id = request.getParameter("id");
		int result = smallClassService.del(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/indexView", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> indexView(HttpServletRequest request) throws InterruptedException {
		String id = request.getParameter("id");
		int result = smallClassService.indexView(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

}
